﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class Manger_Majors : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSourceID = "";
            GridView1.DataBind();
        }

    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        GridView1.DataSourceID = "SqlDataSource1";
        GridView1.DataBind();

        string sMajorID=e.Values["Major_ID"].ToString();
        string sMajorName=e.Values["Major_Name"].ToString();
        BuildNewsTypeXML(sMajorID,sMajorName);
        BuildResourceTypeXML(sMajorID, sMajorName);
        
    }
    /// <summary>
    /// 创建文章类型的XML文件。
    /// 先复制模板中的类型，并添加到数据库。
    /// 生成XML文件，并保存。
    /// </summary>
    public void BuildNewsTypeXML(string sMajorID,string sMajorName)
    {

        SqlDataSource2.InsertCommand = "insert into [zyk_NewsType]([NewsType_Name],[NewsType_Major],[NewsType_Parent]) select [NewsType_Name],@majorid,0 from [zyk_NewsType] where [NewsType_Major]='000000'and [NewsType_Parent]=0";
        SqlDataSource2.InsertParameters.Clear();
        SqlDataSource2.InsertParameters.Add("majorid", sMajorID);
        SqlDataSource2.Insert();

        SqlDataSource2.SelectCommand = "select [NewsType_ID],[NewsType_Name] from [zyk_NewsType] where [NewsType_Major]=@majorid";
        SqlDataSource2.SelectParameters.Clear();
        SqlDataSource2.SelectParameters.Add("majorid", sMajorID);
        DataView dv=(DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
        string sXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        sXML += "<siteMapNode typename=\""+sMajorName+"\" typeid=\"0\">\n";
        for (int i = 0; i < dv.Count; i++)
        {
            sXML += "<siteMapNode typename=\""+dv[i][1].ToString()+"\" typeid=\""+dv[i][0].ToString()+"\" />\n";
        }
        sXML += "</siteMapNode>";
        XmlDocument xd = new XmlDocument();
        xd.LoadXml(sXML);
        xd.Save(MapPath("~/xml/NewsType_"+sMajorID+".xml"));    

    }
    /// <summary>
    /// 创建资源类型的XML文件。
    /// 先复制模板中的类型，并添加到数据库。
    /// 生成XML文件，并保存。
    /// </summary>
    public void BuildResourceTypeXML(string sMajorID, string sMajorName)
    {

        SqlDataSource2.InsertCommand = "insert into [zyk_ResourceType]([ResourceType_Name],[ResourceType_Major],[ResourceType_Parent]) select [ResourceType_Name],@majorid,0 from [zyk_ResourceType] where [ResourceType_Major]='000000'and [ResourceType_Parent]=0";
        SqlDataSource2.InsertParameters.Clear();
        SqlDataSource2.InsertParameters.Add("majorid", sMajorID);
        SqlDataSource2.Insert();

        SqlDataSource2.SelectCommand = "select [ResourceType_ID],[ResourceType_Name] from [zyk_ResourceType] where [ResourceType_Major]=@majorid";
        SqlDataSource2.SelectParameters.Clear();
        SqlDataSource2.SelectParameters.Add("majorid", sMajorID);
        DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
        string sXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        sXML += "<siteMapNode typename=\"" + sMajorName + "\" typeid=\"0\">\n";
        for (int i = 0; i < dv.Count; i++)
        {
            sXML += "<siteMapNode typename=\"" + dv[i][1].ToString() + "\" typeid=\"" + dv[i][0].ToString() + "\" />\n";
        }
        sXML += "</siteMapNode>";
        XmlDocument xd = new XmlDocument();
        xd.LoadXml(sXML);
        xd.Save(MapPath("~/xml/ResourceType_" + sMajorID + ".xml"));

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        string sMajorID=e.Keys["Major_ID"].ToString();
        //删除改专业下所有文章类型
        SqlDataSource2.DeleteCommand = "delete from [zyk_NewsType] where [NewsType_Major]=@majorid";
        SqlDataSource2.DeleteParameters.Clear();
        SqlDataSource2.DeleteParameters.Add("majorid", sMajorID);
        SqlDataSource2.Delete();

        System.IO.File.Delete(MapPath("~/xml/NewsType_"+sMajorID+".xml"));


        //删除该专业下的所有资源类型
        SqlDataSource2.DeleteCommand = "delete from [zyk_ResourceType] where [ResourceType_Major]=@majorid";
        SqlDataSource2.DeleteParameters.Clear();
        SqlDataSource2.DeleteParameters.Add("majorid", sMajorID);
        SqlDataSource2.Delete();

        System.IO.File.Delete(MapPath("~/xml/ResourceType_" + sMajorID + ".xml"));
    }
}
